package com.mobileClientKernel.android.reactNative.service.reactNativeService.service;

import com.mobileClientKernel.android.serviceManager.network.JJBaseRequestMethod;
import com.mobileClientKernel.android.serviceManager.service.IJJServiceCallBack;
import com.mobileClientKernel.android.serviceManager.servicefactory.JJServiceFactory;
import com.mobileClientKernel.android.serviceLayer.httpTransportService.network.JJTBaseRequest;
import com.mobileClientKernel.android.serviceLayer.httpTransportService.service.JJTService;
import com.mobileClientKernel.android.reactNative.service.reactNativeService.model.JJReactNativeModel;
import com.mobileClientKernel.android.reactNative.service.reactNativeService.network.JJReactNativeRequest;
import com.mobileClientKernel.android.commonLayer.JSONUtil;
import org.json.JSONObject;

public class JJReactNativeService extends JJTService {

    public static String serviceName() {
        return "com.mobileClientKernel.android.reactNative.service.reactNativeService.service.JJReactNativeService";
    }

    static public JJReactNativeService getMe() {
        return (JJReactNativeService) JJServiceFactory.getInstance().getService(JJReactNativeService.serviceName());
    }

    public void requestRN(JSONObject options, IJJServiceCallBack callBack) {
        JJTBaseRequest request = getRNRequest(options);
        request.setContext(JJServiceFactory.getInstance().getContext());
        request.setServiceCallBack(callBack);
        getFeatureSet().startRequest(JJServiceFactory.getInstance().getContext(), request);
    }

    public String getCacheRN(JSONObject options) {
        JJReactNativeModel model = (JJReactNativeModel) getRNRequest(options).getCacheModel();
        if (null == model) {
            return null;
        }

        String result = model.getRnDataContent();
        return result;
    }

    public void saveCacheRN(JSONObject options, String object) {
        if (null == object) {
            return;
        }
        JJTBaseRequest request = getRNRequest(options);
        JJReactNativeModel model = new JJReactNativeModel();
        model.setRnDataContent(object);
        request.saveObjectToDisk(model);
    }

    public void removeCacheRN(JSONObject options) {
        getRNRequest(options).removeAllCache();
    }

    public JJTBaseRequest requestForReactNativeWithIdentity(String identity, org.json.JSONObject parameters, boolean isEncryption, Class modelClass, boolean isSaveToMemory, boolean isSaveToDisk) {
        JJReactNativeRequest request = new JJReactNativeRequest(identity, parameters, isEncryption, modelClass, isSaveToMemory, isSaveToDisk);
        return request;
    }

    private JJTBaseRequest getRNRequest(JSONObject options) {
        String userCacheDirectory = JSONUtil.getString(options, "userCacheDirectory");
        String identity = JSONUtil.getString(options, "identity");
        String methodType = JSONUtil.getString(options, "methodType");
        boolean isEncryption = JSONUtil.getBoolean(options, "isEncryption");
        boolean isSaveToDisk = JSONUtil.getBoolean(options, "isSaveToDisk");
        JSONObject parameters = JSONUtil.getJSONObject(options, "parameters");
        JSONObject parametersForSavedFileName = JSONUtil.getJSONObject(options, "parametersForSavedFileName");
        String sensitiveDataForSavedFileName = JSONUtil.getString(options, "sensitiveDataForSavedFileName");
        JSONObject otherInfo = JSONUtil.getJSONObject(options, "otherInfo");

        JJTBaseRequest request = requestForReactNativeWithIdentity(identity, parameters, isEncryption, JJReactNativeModel.class, false, isSaveToDisk);

        if (userCacheDirectory != null) {
            request.setUserCacheDirectory(userCacheDirectory);
        }

        if (parametersForSavedFileName != null) {
            request.setParametersForSavedFileName(parametersForSavedFileName);
        }

        if (methodType != null && methodType.equals("get")) {
            request.setRequestMethod(JJBaseRequestMethod.Get);
        }

        if (sensitiveDataForSavedFileName != null) {
            request.setSensitiveDataForSavedFileName(sensitiveDataForSavedFileName);
        }

        if (otherInfo != null) {
            request.setOtherInfo(otherInfo);
        }

        return request;
    }
}
